<template>
    <view class="pages">
        <Header special="true" :background="background" :title="title" />
        <NavBar :activeIndex="1"/>
        <CheckScreen
            :screenList="screenList"
            :screenIndex="screenIndex"
            @checkChange="checkChange"
            @sortChange="sortChange"
            @industryChange="industryChange"
            @provinceChange="provinceChange"
            @otherChange="otherChange"
        />
        <view class="main" :style="{'min-height': `${noBarHeight - (footerRail ? 130 : 102)}px`}">
            <scroll-view
                class="pro_main"
                scroll-y="true"
                :style="{'height': `${noBarHeight - (footerRail ? 130 : 102)}px`}"
                :enhanced="true"
                :show-scrollbar="false"
                @scrolltolower="lower"
            >
                <CompanyBlack
                    @onClick="navDetail"
                    v-for="(item, index) in dataList"
                    :key="index"
                    :dataDetail="item"
                />
                <NoMessage  v-if="dataList.length == 0"/>
            </scroll-view>

        </view>
    </view>
</template>
<script>

import {loginCheck} from "../../utils/utils";

var app = getApp()
import Header from "@/components/Header.vue"
import NavBar from "@/components/NavBar.vue";
import CheckScreen from "@/components/CheckScreen.vue"
import NoMessage from "@/components/NoMessage.vue";
import CompanyBlack from "@/components/CompanyBlock.vue"
import {mobsearch, userCollect} from "@/utils/api/company";
export default ({
    name: "company",
    data() {
        return {
            background: 'linear-gradient(0, #5162FD, #8E4EF7)',
            title: '企业',
            noBarHeight: app.globalData.noBarHeight,
            footerRail: app.globalData.footerRail,
            screenIndex: 0,
            pages: {
                total: 0,
                size: 20,
                page: 1,
                isOver: false
            },
            dataList: [],
            screenList: [
                {
                    id: 1,
                    title: '智能排序',
                    key: 'sort'
                },
                {
                    id: 2,
                    title: '产业方向',
                    key: 'industry'
                },
                {
                    id: 3,
                    title: '意向区域',
                    key: 'address'
                },
                {
                    id: 4,
                    title: '其他条件',
                    key: 'other'
                }
            ],
            sortValue: {}, // 排序方式
            industryValue: {}, // 产业方式
            provinceValue: {}, // 区域
            otherForm: {
                market: [], // 名称，名称
                tag: [],  // id,id
                nature: [], // id
                scale: [], // id
                crew: [], // id
                registeredcapital: [], // id
                registerdate: [], // id
                abroad: [], // id
            }, // 其他选项数据
        }
    },
    components: {
        NavBar,
        Header,
        CheckScreen,
        CompanyBlack,
        NoMessage
    },
    onLoad() {
        this.getList()
    },
    onShow() {
        loginCheck()
    },
    methods: {
        sortChange(e) {
            this.sortValue = e
            this.screenList[0].title = e.label
            this.screenList[0].img = e.img
            this.screenList[0].inImg = e.inImg
            this.resetting()
            this.getList()
        },
        industryChange(e) {
            this.industryValue = e
            this.screenList[1].title = e.cat_name
            this.resetting()
            this.getList()
        },
        provinceChange(e) {
            this.provinceValue = e
            this.screenList[2].title = e.city_name
            this.resetting()
            this.getList()
        },
        otherChange(e) {
            console.log(e, 'e===')
            this.otherForm = e
            this.resetting()
            this.getList()
        },
        navDetail(e) {
            uni.navigateTo({
                url: `../../detail/pages/companyDetail/companyDetail?id=${e.id}`
            })
        },
        lower() {
            if (!this.pages.isOver) {
                this.pages.page++
                this.getList()
            }
        },
        // 获取企业列表
        async getList() {
            const params = {
                uid: uni.getStorageSync('uid'),
                searchtype: 'firm',
                totalrevenue: 1,
                totalprofitrate: 1,
                registerdateorderby: 1,
                registeredcapitalorderby: 1,
                scaleorderby: 1,
                abroadorderby: 1,
                investmentorderby: 1,
                ratioorderby: 1,
                totalassets: 1
            }
            if (this.sortValue?.key && this.sortValue?.value) {
                params[`${this.sortValue.key}`] = this.sortValue.value
            }
            if (this.industryValue?.id) {
                params.hottrade = this.industryValue.id
            }
            if (this.provinceValue.id) {
                params.province = this.provinceValue.id
            }
            // 其他筛选条件数据
            if (this.otherForm.market.length > 0) {
                params.market = this.otherForm.market.map(res => {
                    return res.cat_name
                }).join(',')
            }
            if (this.otherForm.crew.length > 0) {
                params.crew = this.otherForm.crew.map(res => {
                    return res.id
                }).join(',')
            }
            if (this.otherForm.tag.length > 0) {
                params.tag = this.otherForm.tag.map(res => {
                    return res.id
                }).join(',')
            }
            if (this.otherForm.scale.length > 0) {
                params.scale = this.otherForm.scale.map(res => {
                    return res.id
                }).join(',')
            }
            if (this.otherForm.nature.length > 0) {
                params.nature = this.otherForm.nature.map(res => {
                    return res.id
                }).join(',')
            }
            if (this.otherForm.registeredcapital.length > 0) {
                params.registeredcapital = this.otherForm.registeredcapital.map(res => {
                    return res.id
                }).join(',')
            }
            if (this.otherForm.registerdate.length > 0) {
                params.registerdate = this.otherForm.registerdate.map(res => {
                    return res.id
                }).join(',')
            }
            if (this.otherForm.abroad.length > 0) {
                params.abroad = this.otherForm.abroad.map(res => {
                    return res.id
                }).join(',')
            }
            const pages = {
                p: this.pages.page,
                page: this.pages.size
            }
            let data = {}
            data = await mobsearch(params, pages)
            if (data.data.list) {
                this.dataList = this.dataList.concat(data.data.list)
            }
            this.pages.total = data.data.total
            if (data.data.total < (data.data.page * data.data.p)) {
                this.pages.isOver = true
            }
        },
        checkChange(e) {
            this.screenIndex = e
        },

        // 重置分页信息
        resetting() {
            this.pages = {
                total: 0,
                size: 20,
                page: 1,
                isOver: false
            }
            this.dataList = []
        },
    }
})
</script>

<style scoped lang="scss">
.pages {
    width: 100vw;
}
.main {
    flex-direction: column;
    width: 100%;
    background: #f8f8f8;
    display: flex;
    box-sizing: border-box;
    align-items: center;
    .pro_main {
        width: 100%;
        padding: 12rpx 12rpx 0;
        box-sizing: border-box;
    }
}
</style>
